package com.lza.controller;

import com.lza.Vo.MenuNodeVO;
import com.lza.Vo.UserInfoVO;
import com.lza.entity.Role;
import com.lza.entity.User;
import com.lza.response.ResponseBean;
import com.lza.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lizhaoao
 * @since 2021-10-12
 */
@RestController
@RequestMapping("/user")
@Api(value = "系统用户模块", tags = "系统用户接口")
public class UserController {
    @Autowired
    UserService userService;
    @ApiOperation("用户登陆")
    @PostMapping("/login")
    public ResponseBean login( String username,
                              String password,
                              HttpServletRequest request) {
        System.out.println(username+password);
        String token=userService.login(username,password);
        return ResponseBean.success(token);
    }
    @ApiOperation("用户列表")
    @PostMapping("/findAllUser")//参数一是当前页，参数二是每页个数
    public ResponseBean findAllUser(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize",defaultValue = "7") Integer pageSize) {
       List<User> allUser = userService.findAllUser(pageNum, pageSize);
        return ResponseBean.success(allUser);
    }

    //根据用户id找用户角色,登陆信息者的
    @ApiOperation("用户角色")
    @PostMapping("/findRolesById")
    public ResponseBean findAllUser(Long id) {
        List<Role> roles = userService.findRolesById(id);
        return ResponseBean.success(roles);
    }


    /*//根据用户角色查找用户菜单,这个是集体的,查全部的
    @ApiOperation("根据用户角色查找用户菜单")
    @PostMapping("/findMenuByRoles")
    public ResponseBean findMenuByRoles() {
        List<Menu> menuByRoles = userService.findMenuByRoles();
        return ResponseBean.success(menuByRoles);
    }*/
    /**
     * 登入的用户信息
     *
     * @return
     */
    @ApiOperation(value = "用户信息", notes = "用户登入信息")
    @GetMapping("/info")
   // @Cacheable(cacheNames = "userInfo")//该注解作用:会先查询缓存，如果缓存存在，则不会执行代码块。
    public ResponseBean info() {
        UserInfoVO userInfoVO=userService.info();
        return ResponseBean.success(userInfoVO);
    }
    /**
     * 加载菜单
     *
     * @return
     */
    @ApiOperation(value = "加载菜单", notes = "用户登入后,根据角色加载菜单树")
    @GetMapping("/findMenu")
    public ResponseBean findMenu() {
        List<MenuNodeVO> menuTreeVOS = userService.findMenu();
        return ResponseBean.success(menuTreeVOS);
    }

}

